package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.entity.GrowthData;
import com.xuedongzhilian.service.IGrowthDataService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 个人成长数据控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/growth-data")
public class GrowthDataController {

    @Resource
    private IGrowthDataService growthDataService;

    /**
     * 获取个人成长数据统计
     */
    @GetMapping("/statistics")
    public Result getGrowthStatistics(@RequestParam(required = false) Integer days) {
        Long userId = UserHolder.getUser().getId();
        
        // 默认获取最近30天的数据
        if (days == null) {
            days = 30;
        }
        
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(days - 1);
        
        List<GrowthData> growthDataList = growthDataService.lambdaQuery()
                .eq(GrowthData::getUserId, userId)
                .ge(GrowthData::getDate, startDate)
                .le(GrowthData::getDate, endDate)
                .orderByAsc(GrowthData::getDate)
                .list();
                
        return Result.ok(growthDataList);
    }

    /**
     * 获取数据可视化图表数据
     */
    @GetMapping("/chart-data")
    public Result getChartData(@RequestParam(required = false) Integer days) {
        Long userId = UserHolder.getUser().getId();
        
        // 默认获取最近30天的数据
        if (days == null) {
            days = 30;
        }
        
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(days - 1);
        
        List<GrowthData> growthDataList = growthDataService.lambdaQuery()
                .eq(GrowthData::getUserId, userId)
                .ge(GrowthData::getDate, startDate)
                .le(GrowthData::getDate, endDate)
                .orderByAsc(GrowthData::getDate)
                .list();
                
        // 构造图表数据
        Map<String, Object> chartData = new HashMap<>();
        chartData.put("dates", growthDataList.stream().map(GrowthData::getDate).collect(Collectors.toList()));
        chartData.put("intellectValues", growthDataList.stream().map(GrowthData::getIntellectValue).collect(Collectors.toList()));
        chartData.put("physicalValues", growthDataList.stream().map(GrowthData::getPhysicalValue).collect(Collectors.toList()));
        chartData.put("disciplineValues", growthDataList.stream().map(GrowthData::getDisciplineValue).collect(Collectors.toList()));
        chartData.put("studyTimes", growthDataList.stream().map(GrowthData::getStudyTime).collect(Collectors.toList()));
        chartData.put("runningDistances", growthDataList.stream().map(GrowthData::getRunningDistance).collect(Collectors.toList()));
        chartData.put("bmiValues", growthDataList.stream().map(GrowthData::getBmi).collect(Collectors.toList()));
                
        return Result.ok(chartData);
    }

    /**
     * 更新个人成长数据
     */
    @PostMapping
    public Result updateGrowthData(@RequestBody GrowthData growthData) {
        growthData.setUserId(UserHolder.getUser().getId());
        growthData.setCreateTime(java.time.LocalDateTime.now());
        growthData.setUpdateTime(java.time.LocalDateTime.now());
        
        boolean success = growthDataService.saveOrUpdate(growthData);
        if (success) {
            return Result.ok(growthData);
        } else {
            return Result.fail("更新失败");
        }
    }
}